package com.woniuxy.microsportsserver.walkmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.bean.PageDto;
import com.woniuxy.microsportsserver.bean.WalkAnalysisInfo;
import com.woniuxy.microsportsserver.bean.WalkInfo;
import com.woniuxy.microsportsserver.util.KeyUtil;
import com.woniuxy.microsportsserver.walkmag.mapper.WalkMapper;
import com.woniuxy.microsportsserver.walkmag.service.IWalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class WalkServiceImpl implements IWalkService {
    @Resource
    private WalkMapper walkMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;





    @Override
    public PageDto findAll(WalkInfo walkInfo , Integer pageNum, Integer pageSize)  {
        if(pageNum<=0){
            return null;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<WalkInfo> data = walkMapper.findAll(walkInfo);
        PageInfo<WalkInfo> pageInfo = new PageInfo<>(data);

        PageDto pageDto = new PageDto();
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setData(data);
        return pageDto;
    }

    @Override
    public List<WalkInfo> findRecordsByUserIdOrTime(WalkInfo walkInfo) {
        return walkMapper.findRecordsByUserId(walkInfo);
    }

    @Override
    public List<WalkAnalysisInfo> findTop10ByTimeType(WalkAnalysisInfo walkAnalysisInfo) {
      /*  int type = walkAnalysisInfo.getType();
        if (type == 1){
            return walkMapper.findTop10ByTimeType1(walkAnalysisInfo);
        }else if (type == 2){
            return walkMapper.findTop10ByTimeType2(walkAnalysisInfo);
        }else if(type == 3){
            return walkMapper.findTop10ByTimeType3(walkAnalysisInfo);
        }else {
            return null;
        }*/
        return walkMapper.findTop10ByTimeType1(walkAnalysisInfo);
    }

    @Override
    public List<WalkAnalysisInfo> findNumAvgBytimeType() {
        //1.生成key   自己调方法
        String key = KeyUtil.generate(this, "findNumAvgBytimeType");
        System.out.println(key);

        //2.应该先判断redis中有没有，有直接获取返回
        if (redisTemplate.hasKey(key)) {
            List<Object> objects = redisTemplate.opsForList().range(key, 0, redisTemplate.opsForList().size(key));

            //返回数据
            return  (List<WalkAnalysisInfo>)objects.get(0);

        }else {
            //3.没有则执行SQL  将数据存放到redis  设置过期时间
            // 通过jid到字典中将对应的所有did,也就是找到了该阶段对应的所有视频
       //     List<Integer> dids = dictionaryService.findDidsByJid(jid);

            //去视频表中找到did在dids中的视频
            // where did in (1,2,3,4,5,6)
            List<WalkAnalysisInfo> data = walkMapper.findNumAvgBytimeType();

            //放入redis 设置时间
            redisTemplate.opsForList().leftPushAll(key, data);
            redisTemplate.expire(key, 2000, TimeUnit.SECONDS);

            return data;
        }
    }

    @Override
    public List<WalkAnalysisInfo> findWalkPercentageBYUser() {
        return walkMapper.findWalkPercentageBYUser();
    }
}
